<?php
/* $Id: graph.php 1691 2008-08-15 17:41:51Z bernardli $ */
include_once "./conf.php";
include_once "./functions.php";
include_once "./rrdwrapper.php";
include_once "./mrjob.php";
include_once "./get_context.php";

$graph = new RRDGraph();
$size = $optvals["size"];
if ($size == NULL) { $size = "default"; }
$graph->setOpt("height", $graph_sizes[$size]["height"]);
$graph->setOpt("width", $graph_sizes[$size]["width"]);
$graph->setOpt("font", $graph_sizes[$size]["font"]);
$jobid = $optvals["jobid"];
$metname = $optvals["metric"];
if ($optvals["start"] == NULL ||
    $optvals["end"] == NULL) {
  $start = $time_ranges['hour'] * -1;
  $graph->setOpt("start", $start);
} else {
  $graph->setOpt("start", $optvals["start"]);
  $graph->setOpt("end", $optvals["end"]);
}
$type = $optvals['type'];
$jobsdir=$rrds."/mrjob";
if ($type == "jobcluster") {
  $graph->setOpt("title", $metric_display[$metname]);
  $myjobdir = $jobsdir."/$jobid";
  $hostlist = getDirEntryList($myjobdir);
  $graph->setOpt("vlabel", $metric_unit[$metname]);
  $ret = getJobClusterView($jobid, $metname, $hostlist);
  $graph->setDataDef($ret[0]);
  $graph->setElems(getElems($ret[1], $ret[2]));
} else {
  $hostlist = array($optvals["host"]);
  $graph->setOpt("title", $optvals["host"]);
  $graph->setOpt("vlabel", $metric_unit[$metname]);
  $ret = getJobNodeViewNew($jobid, $optvals["host"], $metname);
  $graph->setDataDef($ret[0]);
  $graph->setElems(getElems($ret[1], $ret[2]));
}
$format = $optvals['imgformat'];
if ($format == NULL) { $format = "PNG"; }
$graph->setOpt('imgformat', $format);

# $debug = 3;
# Did we generate a command?   Run it.
$command = $graph->genCmd();
if($command) {
    /*Make sure the image is not cached*/
    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   // Date in the past
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
    header ("Cache-Control: no-cache, must-revalidate");   // HTTP/1.1
    header ("Pragma: no-cache");                     // HTTP/1.0
    if ($debug>2) {
        header ("Content-type: text/html");
        print "<html><body>";
        print htmlentities( $command );
        print "</body></html>";
    } else {
        header ("Content-type: image/$format");
        passthru($command);
    }
}

?>
